package com.ywjh.inter.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.ywjh.inter.conn.DBConnection;
import com.ywjh.inter.dao.BindInsulinDAO;
import com.ywjh.inter.dao.DataInsulinDAO;
import com.ywjh.inter.dao.EquInsulinStatDAO;
import com.ywjh.inter.dao.InsulinOrderDAO;
import com.ywjh.inter.dao.ProfileDAO;
import com.ywjh.inter.entity.BindInsulinEntity;
import com.ywjh.inter.entity.DataInsulinEntity;
import com.ywjh.inter.entity.EquInsulinStatEntity;
import com.ywjh.inter.entity.InsulinOrderEntity;
import com.ywjh.inter.entity.ProfileEntity;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONObject;

public class InsulinService {
	private static InsulinService in;
	private BindInsulinDAO bindInsulinDAO;
	private DataInsulinDAO insulinDAO;
	private InsulinOrderDAO insulinOrderDAO;
	private ProfileDAO profileDAO;
	private EquInsulinStatDAO equInsulinStatDAO;
	private JSONObject commonData = new JSONObject();
	public static InsulinService getInstance() {
		if (in == null) {
			in = new InsulinService();
			in.bindInsulinDAO=new BindInsulinDAO();
			in.insulinDAO=new DataInsulinDAO();
			in.insulinOrderDAO=new InsulinOrderDAO();
			in.profileDAO=new ProfileDAO();
			in.equInsulinStatDAO=new EquInsulinStatDAO();
			in.init();
		}
		return in;
	}
	
	public boolean rsynInsulinEquStat(EquInsulinStatEntity entity) {
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			EquInsulinStatEntity old = equInsulinStatDAO.findByEquNo(conn, entity.getEqu_no());
			if (old == null) {
				equInsulinStatDAO.insert(conn, entity);
			} else {
				entity.setId(old.getId());
				equInsulinStatDAO.update(conn, entity);
			}
			return true;
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return false;
	}

	public EquInsulinStatEntity findEquInsulinByEquNo(String equ_no) {
		EquInsulinStatEntity entity = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			entity = equInsulinStatDAO.findByEquNo(conn, equ_no);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return entity;
	}
	
	private void init() {

		in.commonData.put("insulin_48B_6",
				"00:00-03:00=0.1,03:00-09:00=0.4,09:00-12:00=0.3,12:00-16:00=0.2,16:00-20:00=0.3,20:00-00:00=0.1");
		in.commonData.put("insulin_48B_8",
				"00:00-03:00=0.2,03:00-09:00=0.5,09:00-12:00=0.3,12:00-16:00=0.3,16:00-20:00=0.4,20:00-00:00=0.2");
		in.commonData.put("insulin_48B_10",
				"00:00-03:00=0.3,03:00-09:00=0.6,09:00-12:00=0.4,12:00-16:00=0.3,16:00-20:00=0.5,20:00-00:00=0.3");
		in.commonData.put("insulin_48B_12",
				"00:00-03:00=0.4,03:00-09:00=0.7,09:00-12:00=0.5,12:00-16:00=0.4,16:00-20:00=0.5,20:00-00:00=0.4");
		in.commonData.put("insulin_48B_14",
				"00:00-03:00=0.4,03:00-09:00=0.8,09:00-12:00=0.6,12:00-16:00=0.6,16:00-20:00=0.6,20:00-00:00=0.5");
		in.commonData.put("insulin_48B_16",
				"00:00-03:00=0.5,03:00-09:00=0.8,09:00-12:00=0.7,12:00-16:00=0.6,16:00-20:00=0.7,20:00-00:00=0.6");
		in.commonData.put("insulin_48B_18",
				"00:00-03:00=0.5,03:00-09:00=1.0,09:00-12:00=0.7,12:00-16:00=0.7,16:00-20:00=0.8,20:00-00:00=0.6");
		in.commonData.put("insulin_48B_20",
				"00:00-03:00=0.6,03:00-09:00=1.1,09:00-12:00=0.8,12:00-16:00=0.7,16:00-20:00=0.9,20:00-00:00=0.7");
		in.commonData.put("insulin_48B_22",
				"00:00-03:00=0.6,03:00-09:00=1.2,09:00-12:00=0.9,12:00-16:00=0.8,16:00-20:00=1.0,20:00-00:00=0.8");
		in.commonData.put("insulin_48B_24",
				"00:00-03:00=0.7,03:00-09:00=1.3,09:00-12:00=1.0,12:00-16:00=0.9,16:00-20:00=1.1,20:00-00:00=0.8");
		in.commonData.put("insulin_48B_26",
				"00:00-03:00=0.8,03:00-09:00=1.4,09:00-12:00=1.1,12:00-16:00=1.0,16:00-20:00=1.1,20:00-00:00=0.9");
		in.commonData.put("insulin_48B_28",
				"00:00-03:00=0.9,03:00-09:00=1.5,09:00-12:00=1.2,12:00-16:00=1.1,16:00-20:00=1.2,20:00-00:00=0.9");
		in.commonData.put("insulin_48B_30",
				"00:00-03:00=0.9,03:00-09:00=1.6,09:00-12:00=1.2,12:00-16:00=1.2,16:00-20:00=1.3,20:00-00:00=1.0");
		in.commonData.put("insulin_48B_32",
				"00:00-03:00=1.0,03:00-09:00=1.6,09:00-12:00=1.4,12:00-16:00=1.3,16:00-20:00=1.4,20:00-00:00=1.1");
		in.commonData.put("insulin_48B_34",
				"00:00-03:00=1.1,03:00-09:00=1.7,09:00-12:00=1.5,12:00-16:00=1.3,16:00-20:00=1.5,20:00-00:00=1.2");
		in.commonData.put("insulin_48B_36",
				"00:00-03:00=1.1,03:00-09:00=1.8,09:00-12:00=1.5,12:00-16:00=1.5,16:00-20:00=1.6,20:00-00:00=1.2");
		in.commonData.put("insulin_48B_38",
				"00:00-03:00=1.2,03:00-09:00=1.9,09:00-12:00=1.6,12:00-16:00=1.5,16:00-20:00=1.7,20:00-00:00=1.3");
		in.commonData.put("insulin_48B_40",
				"00:00-03:00=1.2,03:00-09:00=2.0,09:00-12:00=1.7,12:00-16:00=1.6,16:00-20:00=1.8,20:00-00:00=1.4");

		in.commonData.put("insulin_48A_6",
				"00:00-03:00=0.1,03:00-07:00=0.4,07:00-12:00=0.3,12:00-16:00=0.2,16:00-20:00=0.3,20:00-00:00=0.1");
		in.commonData.put("insulin_48A_8",
				"00:00-03:00=0.2,03:00-07:00=0.5,07:00-12:00=0.3,12:00-16:00=0.3,16:00-20:00=0.4,20:00-00:00=0.2");
		in.commonData.put("insulin_48A_10",
				"00:00-03:00=0.3,03:00-07:00=0.6,07:00-12:00=0.4,12:00-16:00=0.3,16:00-20:00=0.5,20:00-00:00=0.3");
		in.commonData.put("insulin_48A_12",
				"00:00-03:00=0.4,03:00-07:00=0.7,07:00-12:00=0.5,12:00-16:00=0.4,16:00-20:00=0.5,20:00-00:00=0.4");
		in.commonData.put("insulin_48A_14",
				"00:00-03:00=0.4,03:00-07:00=0.8,07:00-12:00=0.6,12:00-16:00=0.6,16:00-20:00=0.6,20:00-00:00=0.5");
		in.commonData.put("insulin_48A_16",
				"00:00-03:00=0.5,03:00-07:00=0.8,07:00-12:00=0.7,12:00-16:00=0.6,16:00-20:00=0.7,20:00-00:00=0.6");
		in.commonData.put("insulin_48A_18",
				"00:00-03:00=0.5,03:00-07:00=1.0,07:00-12:00=0.7,12:00-16:00=0.7,16:00-20:00=0.8,20:00-00:00=0.6");
		in.commonData.put("insulin_48A_20",
				"00:00-03:00=0.6,03:00-07:00=1.1,07:00-12:00=0.8,12:00-16:00=0.7,16:00-20:00=0.9,20:00-00:00=0.7");
		in.commonData.put("insulin_48A_22",
				"00:00-03:00=0.6,03:00-07:00=1.2,07:00-12:00=0.9,12:00-16:00=0.8,16:00-20:00=1.0,20:00-00:00=0.8");
		in.commonData.put("insulin_48A_24",
				"00:00-03:00=0.7,03:00-07:00=1.3,07:00-12:00=1.0,12:00-16:00=0.9,16:00-20:00=1.1,20:00-00:00=0.8");
		in.commonData.put("insulin_48A_26",
				"00:00-03:00=0.8,03:00-07:00=1.4,07:00-12:00=1.1,12:00-16:00=1.0,16:00-20:00=1.1,20:00-00:00=0.9");
		in.commonData.put("insulin_48A_28",
				"00:00-03:00=0.9,03:00-07:00=1.5,07:00-12:00=1.2,12:00-16:00=1.1,16:00-20:00=1.2,20:00-00:00=0.9");
		in.commonData.put("insulin_48A_30",
				"00:00-03:00=0.9,03:00-07:00=1.6,07:00-12:00=1.2,12:00-16:00=1.2,16:00-20:00=1.3,20:00-00:00=1.0");
		in.commonData.put("insulin_48A_32",
				"00:00-03:00=1.0,03:00-07:00=1.6,07:00-12:00=1.4,12:00-16:00=1.3,16:00-20:00=1.4,20:00-00:00=1.1");
		in.commonData.put("insulin_48A_34",
				"00:00-03:00=1.1,03:00-07:00=1.7,07:00-12:00=1.5,12:00-16:00=1.3,16:00-20:00=1.5,20:00-00:00=1.2");
		in.commonData.put("insulin_48A_36",
				"00:00-03:00=1.1,03:00-07:00=1.8,07:00-12:00=1.5,12:00-16:00=1.5,16:00-20:00=1.6,20:00-00:00=1.2");
		in.commonData.put("insulin_48A_38",
				"00:00-03:00=1.2,03:00-07:00=1.9,07:00-12:00=1.6,12:00-16:00=1.5,16:00-20:00=1.7,20:00-00:00=1.3");
		in.commonData.put("insulin_48A_40",
				"00:00-03:00=1.2,03:00-07:00=2.0,07:00-12:00=1.7,12:00-16:00=1.6,16:00-20:00=1.8,20:00-00:00=1.4");

		in.commonData.put("insulin_suxiao_6",
				"00:00-01:00=0.1,01:00-02:00=0.1,02:00-03:00=0.2,03:00-04:00=0.2,04:00-05:00=0.3,05:00-06:00=0.4,06:00-07:00=0.6,07:00-08:00=0.4,08:00-09:00=0.2,09:00-10:00=0.3,10:00-11:00=0.2,11:00-12:00=0.2,12:00-13:00=0.2,13:00-14:00=0.2,14:00-15:00=0.2,15:00-16:00=0.2,16:00-17:00=0.3,17:00-18:00=0.3,18:00-19:00=0.3,19:00-20:00=0.3,20:00-21:00=0.2,21:00-22:00=0.2,22:00-23:00=0.2,23:00-00:00=0.2");
		in.commonData.put("insulin_suxiao_8",
				"00:00-01:00=0.2,01:00-02:00=0.2,02:00-03:00=0.2,03:00-04:00=0.3,04:00-05:00=0.5,05:00-06:00=0.6,06:00-07:00=0.7,07:00-08:00=0.6,08:00-09:00=0.4,09:00-10:00=0.3,10:00-11:00=0.2,11:00-12:00=0.2,12:00-13:00=0.2,13:00-14:00=0.2,14:00-15:00=0.2,15:00-16:00=0.3,16:00-17:00=0.4,17:00-18:00=0.5,18:00-19:00=0.5,19:00-20:00=0.3,20:00-21:00=0.3,21:00-22:00=0.3,22:00-23:00=0.2,23:00-00:00=0.2");
		in.commonData.put("insulin_suxiao_10",
				"00:00-01:00=0.2,01:00-02:00=0.2,02:00-03:00=0.3,03:00-04:00=0.4,04:00-05:00=0.5,05:00-06:00=0.7,06:00-07:00=0.8,07:00-08:00=0.7,08:00-09:00=0.5,09:00-10:00=0.3,10:00-11:00=0.3,11:00-12:00=0.3,12:00-13:00=0.3,13:00-14:00=0.3,14:00-15:00=0.3,15:00-16:00=0.4,16:00-17:00=0.5,17:00-18:00=0.6,18:00-19:00=0.6,19:00-20:00=0.5,20:00-21:00=0.4,21:00-22:00=0.3,22:00-23:00=0.3,23:00-00:00=0.3");
		in.commonData.put("insulin_suxiao_12",
				"00:00-01:00=0.2,01:00-02:00=0.2,02:00-03:00=0.3,03:00-04:00=0.5,04:00-05:00=0.7,05:00-06:00=0.9,06:00-07:00=1.0,07:00-08:00=0.8,08:00-09:00=0.5,09:00-10:00=0.4,10:00-11:00=0.4,11:00-12:00=0.4,12:00-13:00=0.4,13:00-14:00=0.4,14:00-15:00=0.4,15:00-16:00=0.4,16:00-17:00=0.5,17:00-18:00=0.7,18:00-19:00=0.7,19:00-20:00=0.6,20:00-21:00=0.5,21:00-22:00=0.4,22:00-23:00=0.4,23:00-00:00=0.3");
		in.commonData.put("insulin_suxiao_14",
				"00:00-01:00=0.3,01:00-02:00=0.3,02:00-03:00=0.4,03:00-04:00=0.5,04:00-05:00=0.6,05:00-06:00=0.9,06:00-07:00=1.2,07:00-08:00=0.9,08:00-09:00=0.6,09:00-10:00=0.5,10:00-11:00=0.5,11:00-12:00=0.5,12:00-13:00=0.5,13:00-14:00=0.5,14:00-15:00=0.5,15:00-16:00=0.5,16:00-17:00=0.7,17:00-18:00=0.8,18:00-19:00=0.8,19:00-20:00=0.6,20:00-21:00=0.5,21:00-22:00=0.5,22:00-23:00=0.5,23:00-00:00=0.4");
		in.commonData.put("insulin_suxiao_16",
				"00:00-01:00=0.4,01:00-02:00=0.4,02:00-03:00=0.5,03:00-04:00=0.5,04:00-05:00=0.7,05:00-06:00=1.1,06:00-07:00=1.4,07:00-08:00=1.1,08:00-09:00=0.8,09:00-10:00=0.5,10:00-11:00=0.5,11:00-12:00=0.5,12:00-13:00=0.5,13:00-14:00=0.5,14:00-15:00=0.5,15:00-16:00=0.6,16:00-17:00=0.8,17:00-18:00=0.9,18:00-19:00=0.9,19:00-20:00=0.8,20:00-21:00=0.6,21:00-22:00=0.5,22:00-23:00=0.5,23:00-00:00=0.5");
		in.commonData.put("insulin_suxiao_18",
				"00:00-01:00=0.4,01:00-02:00=0.4,02:00-03:00=0.5,03:00-04:00=0.6,04:00-05:00=0.8,05:00-06:00=1.3,06:00-07:00=1.5,07:00-08:00=1.2,08:00-09:00=0.9,09:00-10:00=0.6,10:00-11:00=0.6,11:00-12:00=0.6,12:00-13:00=0.6,13:00-14:00=0.6,14:00-15:00=0.6,15:00-16:00=0.7,16:00-17:00=0.9,17:00-18:00=1.0,18:00-19:00=1.0,19:00-20:00=0.8,20:00-21:00=0.7,21:00-22:00=0.6,22:00-23:00=0.6,23:00-00:00=0.5");
		in.commonData.put("insulin_suxiao_20",
				"00:00-01:00=0.4,01:00-02:00=0.4,02:00-03:00=0.6,03:00-04:00=0.7,04:00-05:00=0.9,05:00-06:00=1.4,06:00-07:00=1.6,07:00-08:00=1.2,08:00-09:00=1.0,09:00-10:00=0.7,10:00-11:00=0.7,11:00-12:00=0.7,12:00-13:00=0.7,13:00-14:00=0.7,14:00-15:00=0.7,15:00-16:00=0.8,16:00-17:00=1.0,17:00-18:00=1.1,18:00-19:00=1.1,19:00-20:00=0.9,20:00-21:00=0.8,21:00-22:00=0.7,22:00-23:00=0.7,23:00-00:00=0.5");
		in.commonData.put("insulin_suxiao_22",
				"00:00-01:00=0.5,01:00-02:00=0.5,02:00-03:00=0.6,03:00-04:00=0.8,04:00-05:00=1.0,05:00-06:00=1.6,06:00-07:00=1.8,07:00-08:00=1.5,08:00-09:00=1.1,09:00-10:00=0.8,10:00-11:00=0.7,11:00-12:00=0.7,12:00-13:00=0.7,13:00-14:00=0.7,14:00-15:00=0.8,15:00-16:00=0.9,16:00-17:00=1.1,17:00-18:00=1.2,18:00-19:00=1.2,19:00-20:00=1.0,20:00-21:00=0.8,21:00-22:00=0.7,22:00-23:00=0.7,23:00-00:00=0.6");
		in.commonData.put("insulin_suxiao_24",
				"00:00-01:00=0.5,01:00-02:00=0.5,02:00-03:00=0.7,03:00-04:00=0.8,04:00-05:00=1.1,05:00-06:00=1.7,06:00-07:00=2.0,07:00-08:00=1.6,08:00-09:00=1.2,09:00-10:00=0.9,10:00-11:00=0.8,11:00-12:00=0.8,12:00-13:00=0.8,13:00-14:00=0.8,14:00-15:00=0.8,15:00-16:00=1.0,16:00-17:00=1.2,17:00-18:00=1.3,18:00-19:00=1.3,19:00-20:00=1.0,20:00-21:00=0.9,21:00-22:00=0.8,22:00-23:00=0.8,23:00-00:00=0.7");
		in.commonData.put("insulin_suxiao_26",
				"00:00-01:00=0.6,01:00-02:00=0.6,02:00-03:00=0.7,03:00-04:00=0.9,04:00-05:00=1.1,05:00-06:00=1.8,06:00-07:00=2.1,07:00-08:00=1.7,08:00-09:00=1.3,09:00-10:00=1.0,10:00-11:00=0.9,11:00-12:00=0.9,12:00-13:00=0.9,13:00-14:00=0.9,14:00-15:00=1.0,15:00-16:00=1.1,16:00-17:00=1.3,17:00-18:00=1.4,18:00-19:00=1.4,19:00-20:00=1.1,20:00-21:00=1.0,21:00-22:00=0.8,22:00-23:00=0.8,23:00-00:00=0.7");
		in.commonData.put("insulin_suxiao_28",
				"00:00-01:00=0.6,01:00-02:00=0.7,02:00-03:00=0.8,03:00-04:00=0.9,04:00-05:00=1.2,05:00-06:00=1.9,06:00-07:00=2.3,07:00-08:00=1.8,08:00-09:00=1.4,09:00-10:00=1.0,10:00-11:00=1.0,11:00-12:00=1.0,12:00-13:00=1.0,13:00-14:00=1.0,14:00-15:00=1.0,15:00-16:00=1.1,16:00-17:00=1.4,17:00-18:00=1.5,18:00-19:00=1.5,19:00-20:00=1.2,20:00-21:00=1.1,21:00-22:00=0.9,22:00-23:00=0.9,23:00-00:00=0.8");
		in.commonData.put("insulin_suxiao_30",
				"00:00-01:00=0.7,01:00-02:00=0.7,02:00-03:00=0.8,03:00-04:00=1.0,04:00-05:00=1.4,05:00-06:00=2.1,06:00-07:00=2.5,07:00-08:00=2.0,08:00-09:00=1.5,09:00-10:00=1.0,10:00-11:00=1.0,11:00-12:00=1.0,12:00-13:00=1.0,13:00-14:00=1.0,14:00-15:00=1.0,15:00-16:00=1.2,16:00-17:00=1.5,17:00-18:00=1.6,18:00-19:00=1.6,19:00-20:00=1.3,20:00-21:00=1.2,21:00-22:00=1.0,22:00-23:00=1.0,23:00-00:00=0.9");
		in.commonData.put("insulin_suxiao_32",
				"00:00-01:00=0.7,01:00-02:00=0.7,02:00-03:00=0.9,03:00-04:00=1.1,04:00-05:00=1.4,05:00-06:00=2.2,06:00-07:00=2.6,07:00-08:00=2.1,08:00-09:00=1.6,09:00-10:00=1.1,10:00-11:00=1.1,11:00-12:00=1.1,12:00-13:00=1.1,13:00-14:00=1.1,14:00-15:00=1.1,15:00-16:00=1.3,16:00-17:00=1.6,17:00-18:00=1.7,18:00-19:00=1.7,19:00-20:00=1.4,20:00-21:00=1.3,21:00-22:00=1.1,22:00-23:00=1.1,23:00-00:00=0.9");
		in.commonData.put("insulin_suxiao_34",
				"00:00-01:00=0.8,01:00-02:00=0.8,02:00-03:00=1.0,03:00-04:00=1.2,04:00-05:00=1.5,05:00-06:00=2.5,06:00-07:00=2.8,07:00-08:00=2.3,08:00-09:00=1.7,09:00-10:00=1.1,10:00-11:00=1.1,11:00-12:00=1.1,12:00-13:00=1.1,13:00-14:00=1.1,14:00-15:00=1.1,15:00-16:00=1.3,16:00-17:00=1.7,17:00-18:00=1.9,18:00-19:00=1.9,19:00-20:00=1.5,20:00-21:00=1.4,21:00-22:00=1.1,22:00-23:00=1.1,23:00-00:00=0.9");
		in.commonData.put("insulin_suxiao_36",
				"00:00-01:00=0.8,01:00-02:00=0.8,02:00-03:00=1.0,03:00-04:00=1.2,04:00-05:00=1.6,05:00-06:00=2.6,06:00-07:00=3.0,07:00-08:00=2.4,08:00-09:00=1.8,09:00-10:00=1.2,10:00-11:00=1.2,11:00-12:00=1.2,12:00-13:00=1.2,13:00-14:00=1.2,14:00-15:00=1.2,15:00-16:00=1.4,16:00-17:00=1.8,17:00-18:00=2.0,18:00-19:00=2.0,19:00-20:00=1.6,20:00-21:00=1.4,21:00-22:00=1.2,22:00-23:00=1.2,23:00-00:00=1.0");
		in.commonData.put("insulin_suxiao_38",
				"00:00-01:00=0.8,01:00-02:00=0.8,02:00-03:00=1.1,03:00-04:00=1.3,04:00-05:00=1.7,05:00-06:00=2.7,06:00-07:00=3.1,07:00-08:00=2.5,08:00-09:00=1.8,09:00-10:00=1.3,10:00-11:00=1.3,11:00-12:00=1.3,12:00-13:00=1.3,13:00-14:00=1.3,14:00-15:00=1.3,15:00-16:00=1.5,16:00-17:00=1.9,17:00-18:00=2.1,18:00-19:00=2.1,19:00-20:00=1.7,20:00-21:00=1.5,21:00-22:00=1.3,22:00-23:00=1.3,23:00-00:00=1.0");
		in.commonData.put("insulin_suxiao_40",
				"00:00-01:00=0.9,01:00-02:00=0.9,02:00-03:00=1.2,03:00-04:00=1.4,04:00-05:00=1.8,05:00-06:00=2.9,06:00-07:00=3.3,07:00-08:00=2.7,08:00-09:00=2.0,09:00-10:00=1.3,10:00-11:00=1.3,11:00-12:00=1.3,12:00-13:00=1.3,13:00-14:00=1.3,14:00-15:00=1.3,15:00-16:00=1.6,16:00-17:00=2.0,17:00-18:00=2.2,18:00-19:00=2.2,19:00-20:00=1.8,20:00-21:00=1.6,21:00-22:00=1.3,22:00-23:00=1.3,23:00-00:00=1.1");

		in.commonData.put("insulin_duanxiao_6",
				"00:00-01:00=0.1,01:00-02:00=0.2,02:00-03:00=0.2,03:00-04:00=0.3,04:00-05:00=0.4,05:00-06:00=0.6,06:00-07:00=0.4,07:00-08:00=0.3,08:00-09:00=0.2,09:00-10:00=0.2,10:00-11:00=0.2,11:00-12:00=0.2,12:00-13:00=0.2,13:00-14:00=0.2,14:00-15:00=0.2,15:00-16:00=0.3,16:00-17:00=0.3,17:00-18:00=0.3,18:00-19:00=0.3,19:00-20:00=0.2,20:00-21:00=0.2,21:00-22:00=0.2,22:00-23:00=0.2,23:00-00:00=0.1");
		in.commonData.put("insulin_duanxiao_8",
				"00:00-01:00=0.2,01:00-02:00=0.2,02:00-03:00=0.3,03:00-04:00=0.5,04:00-05:00=0.6,05:00-06:00=0.7,06:00-07:00=0.6,07:00-08:00=0.4,08:00-09:00=0.3,09:00-10:00=0.2,10:00-11:00=0.2,11:00-12:00=0.2,12:00-13:00=0.2,13:00-14:00=0.2,14:00-15:00=0.3,15:00-16:00=0.4,16:00-17:00=0.5,17:00-18:00=0.5,18:00-19:00=0.3,19:00-20:00=0.3,20:00-21:00=0.3,21:00-22:00=0.2,22:00-23:00=0.2,23:00-00:00=0.2");
		in.commonData.put("insulin_duanxiao_10",
				"00:00-01:00=0.2,01:00-02:00=0.3,02:00-03:00=0.4,03:00-04:00=0.5,04:00-05:00=0.7,05:00-06:00=0.8,06:00-07:00=0.7,07:00-08:00=0.5,08:00-09:00=0.3,09:00-10:00=0.3,10:00-11:00=0.3,11:00-12:00=0.3,12:00-13:00=0.3,13:00-14:00=0.3,14:00-15:00=0.4,15:00-16:00=0.5,16:00-17:00=0.6,17:00-18:00=0.6,18:00-19:00=0.5,19:00-20:00=0.4,20:00-21:00=0.3,21:00-22:00=0.3,22:00-23:00=0.3,23:00-00:00=0.2");
		in.commonData.put("insulin_duanxiao_12",
				"00:00-01:00=0.2,01:00-02:00=0.3,02:00-03:00=0.5,03:00-04:00=0.7,04:00-05:00=0.9,05:00-06:00=1.0,06:00-07:00=0.8,07:00-08:00=0.5,08:00-09:00=0.4,09:00-10:00=0.4,10:00-11:00=0.4,11:00-12:00=0.4,12:00-13:00=0.4,13:00-14:00=0.4,14:00-15:00=0.4,15:00-16:00=0.5,16:00-17:00=0.7,17:00-18:00=0.7,18:00-19:00=0.6,19:00-20:00=0.5,20:00-21:00=0.4,21:00-22:00=0.4,22:00-23:00=0.3,23:00-00:00=0.2");
		in.commonData.put("insulin_duanxiao_14",
				"00:00-01:00=0.3,01:00-02:00=0.3,02:00-03:00=0.4,03:00-04:00=0.5,04:00-05:00=0.6,05:00-06:00=0.9,06:00-07:00=1.2,07:00-08:00=0.9,08:00-09:00=0.6,09:00-10:00=0.5,10:00-11:00=0.5,11:00-12:00=0.5,12:00-13:00=0.5,13:00-14:00=0.5,14:00-15:00=0.5,15:00-16:00=0.5,16:00-17:00=0.7,17:00-18:00=0.8,18:00-19:00=0.8,19:00-20:00=0.6,20:00-21:00=0.5,21:00-22:00=0.5,22:00-23:00=0.5,23:00-00:00=0.4");
		in.commonData.put("insulin_duanxiao_16",
				"00:00-01:00=0.4,01:00-02:00=0.5,02:00-03:00=0.5,03:00-04:00=0.7,04:00-05:00=1.1,05:00-06:00=1.4,06:00-07:00=1.1,07:00-08:00=0.8,08:00-09:00=0.5,09:00-10:00=0.5,10:00-11:00=0.5,11:00-12:00=0.5,12:00-13:00=0.5,13:00-14:00=0.5,14:00-15:00=0.6,15:00-16:00=0.8,16:00-17:00=0.9,17:00-18:00=0.9,18:00-19:00=0.8,19:00-20:00=0.6,20:00-21:00=0.5,21:00-22:00=0.5,22:00-23:00=0.5,23:00-00:00=0.4");
		in.commonData.put("insulin_duanxiao_18",
				"00:00-01:00=0.4,01:00-02:00=0.5,02:00-03:00=0.6,03:00-04:00=0.8,04:00-05:00=1.3,05:00-06:00=1.5,06:00-07:00=1.2,07:00-08:00=0.9,08:00-09:00=0.6,09:00-10:00=0.6,10:00-11:00=0.6,11:00-12:00=0.6,12:00-13:00=0.6,13:00-14:00=0.6,14:00-15:00=0.7,15:00-16:00=0.9,16:00-17:00=1.0,17:00-18:00=1.0,18:00-19:00=0.8,19:00-20:00=0.7,20:00-21:00=0.6,21:00-22:00=0.6,22:00-23:00=0.5,23:00-00:00=0.4");
		in.commonData.put("insulin_duanxiao_20",
				"00:00-01:00=0.4,01:00-02:00=0.6,02:00-03:00=0.7,03:00-04:00=0.9,04:00-05:00=1.4,05:00-06:00=1.6,06:00-07:00=1.2,07:00-08:00=1.0,08:00-09:00=0.7,09:00-10:00=0.7,10:00-11:00=0.7,11:00-12:00=0.7,12:00-13:00=0.7,13:00-14:00=0.7,14:00-15:00=0.8,15:00-16:00=1.0,16:00-17:00=1.1,17:00-18:00=1.1,18:00-19:00=0.9,19:00-20:00=0.8,20:00-21:00=0.7,21:00-22:00=0.7,22:00-23:00=0.5,23:00-00:00=0.4");
		in.commonData.put("insulin_duanxiao_22",
				"00:00-01:00=0.5,01:00-02:00=0.6,02:00-03:00=0.8,03:00-04:00=1.0,04:00-05:00=1.6,05:00-06:00=1.8,06:00-07:00=1.5,07:00-08:00=1.1,08:00-09:00=0.8,09:00-10:00=0.7,10:00-11:00=0.7,11:00-12:00=0.7,12:00-13:00=0.7,13:00-14:00=0.8,14:00-15:00=0.9,15:00-16:00=1.1,16:00-17:00=1.2,17:00-18:00=1.2,18:00-19:00=1.0,19:00-20:00=0.8,20:00-21:00=0.7,21:00-22:00=0.7,22:00-23:00=0.6,23:00-00:00=0.5");
		in.commonData.put("insulin_duanxiao_24",
				"00:00-01:00=0.5,01:00-02:00=0.7,02:00-03:00=0.8,03:00-04:00=1.1,04:00-05:00=1.7,05:00-06:00=2.0,06:00-07:00=1.6,07:00-08:00=1.2,08:00-09:00=0.9,09:00-10:00=0.8,10:00-11:00=0.8,11:00-12:00=0.8,12:00-13:00=0.8,13:00-14:00=0.8,14:00-15:00=1.0,15:00-16:00=1.2,16:00-17:00=1.3,17:00-18:00=1.3,18:00-19:00=1.0,19:00-20:00=0.9,20:00-21:00=0.8,21:00-22:00=0.8,22:00-23:00=0.7,23:00-00:00=0.5");
		in.commonData.put("insulin_duanxiao_26",
				"00:00-01:00=0.6,01:00-02:00=0.7,02:00-03:00=0.9,03:00-04:00=1.1,04:00-05:00=1.8,05:00-06:00=2.1,06:00-07:00=1.7,07:00-08:00=1.3,08:00-09:00=1.0,09:00-10:00=0.9,10:00-11:00=0.9,11:00-12:00=0.9,12:00-13:00=0.9,13:00-14:00=1.0,14:00-15:00=1.1,15:00-16:00=1.3,16:00-17:00=1.4,17:00-18:00=1.4,18:00-19:00=1.1,19:00-20:00=1.0,20:00-21:00=0.8,21:00-22:00=0.8,22:00-23:00=0.7,23:00-00:00=0.6");
		in.commonData.put("insulin_duanxiao_28",
				"00:00-01:00=0.7,01:00-02:00=0.8,02:00-03:00=0.9,03:00-04:00=1.2,04:00-05:00=1.9,05:00-06:00=2.3,06:00-07:00=1.8,07:00-08:00=1.4,08:00-09:00=1.0,09:00-10:00=1.0,10:00-11:00=1.0,11:00-12:00=1.0,12:00-13:00=1.0,13:00-14:00=1.0,14:00-15:00=1.1,15:00-16:00=1.4,16:00-17:00=1.5,17:00-18:00=1.5,18:00-19:00=1.2,19:00-20:00=1.1,20:00-21:00=0.9,21:00-22:00=0.9,22:00-23:00=0.8,23:00-00:00=0.6");
		in.commonData.put("insulin_duanxiao_30",
				"00:00-01:00=0.7,01:00-02:00=0.8,02:00-03:00=1.0,03:00-04:00=1.4,04:00-05:00=2.1,05:00-06:00=2.5,06:00-07:00=2.0,07:00-08:00=1.5,08:00-09:00=1.0,09:00-10:00=1.0,10:00-11:00=1.0,11:00-12:00=1.0,12:00-13:00=1.0,13:00-14:00=1.0,14:00-15:00=1.2,15:00-16:00=1.5,16:00-17:00=1.6,17:00-18:00=1.6,18:00-19:00=1.3,19:00-20:00=1.2,20:00-21:00=1.0,21:00-22:00=1.0,22:00-23:00=0.9,23:00-00:00=0.7");
		in.commonData.put("insulin_duanxiao_32",
				"00:00-01:00=0.7,01:00-02:00=0.9,02:00-03:00=1.1,03:00-04:00=1.4,04:00-05:00=2.2,05:00-06:00=2.6,06:00-07:00=2.1,07:00-08:00=1.6,08:00-09:00=1.1,09:00-10:00=1.1,10:00-11:00=1.1,11:00-12:00=1.1,12:00-13:00=1.1,13:00-14:00=1.1,14:00-15:00=1.3,15:00-16:00=1.6,16:00-17:00=1.7,17:00-18:00=1.7,18:00-19:00=1.4,19:00-20:00=1.3,20:00-21:00=1.1,21:00-22:00=1.1,22:00-23:00=0.9,23:00-00:00=0.7");
		in.commonData.put("insulin_duanxiao_34",
				"00:00-01:00=0.8,01:00-02:00=1.0,02:00-03:00=1.2,03:00-04:00=1.5,04:00-05:00=2.5,05:00-06:00=2.8,06:00-07:00=2.3,07:00-08:00=1.7,08:00-09:00=1.1,09:00-10:00=1.1,10:00-11:00=1.1,11:00-12:00=1.1,12:00-13:00=1.1,13:00-14:00=1.1,14:00-15:00=1.3,15:00-16:00=1.7,16:00-17:00=1.9,17:00-18:00=1.9,18:00-19:00=1.5,19:00-20:00=1.4,20:00-21:00=1.1,21:00-22:00=1.1,22:00-23:00=0.9,23:00-00:00=0.8");
		in.commonData.put("insulin_duanxiao_36",
				"00:00-01:00=0.8,01:00-02:00=1.0,02:00-03:00=1.2,03:00-04:00=1.6,04:00-05:00=2.6,05:00-06:00=3.0,06:00-07:00=2.4,07:00-08:00=1.8,08:00-09:00=1.2,09:00-10:00=1.2,10:00-11:00=1.2,11:00-12:00=1.2,12:00-13:00=1.2,13:00-14:00=1.2,14:00-15:00=1.4,15:00-16:00=1.8,16:00-17:00=2.0,17:00-18:00=2.0,18:00-19:00=1.6,19:00-20:00=1.4,20:00-21:00=1.2,21:00-22:00=1.2,22:00-23:00=1.0,23:00-00:00=0.8");
		in.commonData.put("insulin_duanxiao_38",
				"00:00-01:00=0.8,01:00-02:00=1.1,02:00-03:00=1.3,03:00-04:00=1.7,04:00-05:00=2.7,05:00-06:00=3.1,06:00-07:00=2.5,07:00-08:00=1.8,08:00-09:00=1.3,09:00-10:00=1.3,10:00-11:00=1.3,11:00-12:00=1.3,12:00-13:00=1.3,13:00-14:00=1.3,14:00-15:00=1.5,15:00-16:00=1.9,16:00-17:00=2.1,17:00-18:00=2.1,18:00-19:00=1.7,19:00-20:00=1.5,20:00-21:00=1.3,21:00-22:00=1.3,22:00-23:00=1.0,23:00-00:00=0.8");
		in.commonData.put("insulin_duanxiao_40",
				"00:00-01:00=0.9,01:00-02:00=1.2,02:00-03:00=1.4,03:00-04:00=1.8,04:00-05:00=2.9,05:00-06:00=3.3,06:00-07:00=2.7,07:00-08:00=2.0,08:00-09:00=1.3,09:00-10:00=1.3,10:00-11:00=1.3,11:00-12:00=1.3,12:00-13:00=1.3,13:00-14:00=1.3,14:00-15:00=1.6,15:00-16:00=2.0,16:00-17:00=2.2,17:00-18:00=2.2,18:00-19:00=1.8,19:00-20:00=1.6,20:00-21:00=1.3,21:00-22:00=1.3,22:00-23:00=1.1,23:00-00:00=0.9");

	}

	public boolean bindInsulin(BindInsulinEntity entity) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			BindInsulinEntity binded = bindInsulinDAO.findBypatientId(conn, entity.getPatient_id());
			if (binded == null) {
				b = bindInsulinDAO.insert(conn, entity);
			} else {
				binded.setStatus(0);
				binded.setAdd_time(new Timestamp(System.currentTimeMillis()));
				binded.setDoc_id(entity.getDoc_id());
				bindInsulinDAO.update(conn, binded);
				b = bindInsulinDAO.insert(conn, entity);
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;
	}

	public BindInsulinEntity findInsulin(String equ_name, String equ_mac) {
		BindInsulinEntity entity = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			entity = bindInsulinDAO.findByEqu(conn, equ_name, equ_mac);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return entity;
	}

	public BindInsulinEntity findInsulinByPatientId(int patiant_id) {
		BindInsulinEntity entity = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			entity = bindInsulinDAO.findBypatientId(conn, patiant_id);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return entity;
	}

	public boolean unbindInsulin(BindInsulinEntity entity) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			b = bindInsulinDAO.update(conn, entity);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;
	}

	public int getBindPatientByInsulinEqu(String equ_name, String equ_mac, Timestamp check_time) {
		int pid = 0;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			pid = bindInsulinDAO.findPatientByEqu(conn, equ_name, equ_mac, check_time);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return pid;
	}
	
	public int getBindPatientByInsulinEqu(String equ_name, String equ_mac) {
		int pid = 0;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			pid = bindInsulinDAO.findPatientByEqu(conn, equ_name, equ_mac);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return pid;
	}
	
	public Map<String, Integer> checkInsulinBind(String[] macs) {
		Map<String, Integer> map = new LinkedHashMap<>();
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			for (String mac : macs) {
				boolean b = bindInsulinDAO.findByMac(conn, mac);
				map.put(mac, b ? 1 : 0);
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return map;
	}
	
	public boolean rsynInsulin(List<DataInsulinEntity> list) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			for (DataInsulinEntity e : list) {
				DataInsulinEntity find = insulinDAO.findByTypeTime(conn, e.getPatient_id(), e.getType(),
						e.getCheck_time(), e.getMemo());
				if (find == null) {
					insulinDAO.insert(conn, e);
				} else {
					e.setId(find.getId());
					insulinDAO.update(conn, e);
				}
			}
			b = true;
		} catch (ClassNotFoundException | SQLException e1) {
			e1.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;
	}
	
	public boolean rsynInsulin(List<DataInsulinEntity> list, int patient_id, int type) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			if (!list.isEmpty()) {
				for (DataInsulinEntity data : list) {
					insulinDAO.deleteDataByType(conn, patient_id, type, data.getCheck_time());
				}
				for (DataInsulinEntity data : list) {
					insulinDAO.insert(conn, data);
				}
				b = true;
			}
		} catch (ClassNotFoundException | SQLException e1) {
			e1.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;
	}

	public String getInsulinCommData(String key) {
		String v = commonData.getString(key);
		return v;
	}
	
	public List<DataInsulinEntity> queryInsulinData(String start_time, String end_time, int patient_id, int type) {
		List<DataInsulinEntity> list = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			list = insulinDAO.query(conn, start_time, end_time, patient_id, type);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}
	
	
	public boolean setInsulinOrder(InsulinOrderEntity e) {
		Connection conn = null;
		boolean b = false;
		try {
			conn = DBConnection.getDBConnection();
			b = insulinOrderDAO.insert(conn, e);
		} catch (ClassNotFoundException | SQLException e1) {
			e1.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;
	}

	/**
	 * 执行完临时追加量后，将临时有效状态设置为无效
	 * 
	 * @return
	 */
	public boolean disableTempInsulin(int patient_id, int type) {
		Connection conn = null;
		boolean b = false;
		try {
			conn = DBConnection.getDBConnection();
			b = insulinOrderDAO.deleteTempByPatientId(conn, patient_id, type);
		} catch (ClassNotFoundException | SQLException e1) {
			e1.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;

	}

	public boolean setInsulinOrder(int doc_id, int patient_id, int type, String[] name, String[] data) {
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			if (data == null || name == null) {
				return false;
			}
			if (data.length != name.length) {
				return false;
			}
			conn.setAutoCommit(false);
			insulinOrderDAO.deleteByPatientId(conn, patient_id, type);
			for (int i = 0; i < name.length; i++) {
				String n = name[i];
				float d = StrUtil.getFloat(data[i], 0.0f);
				InsulinOrderEntity e = new InsulinOrderEntity();
				e.setAdd_time(new Timestamp(System.currentTimeMillis()));
				e.setData(d);
				e.setDoc_id(doc_id);
				e.setName(n);
				e.setPatient_id(patient_id);
				e.setStatus(1);
				e.setType(type);
				insulinOrderDAO.insert(conn, e);
			}
			// 设置通知
			if (type == 2) {
				ProfileEntity profile = profileDAO.findByUserId(conn, patient_id);
				profile.setHasNewInsulinOrder(1);
				profileDAO.update(conn, profile);
			}
			conn.commit();
			return true;
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			DBConnection.close(conn);
		}
		return false;
	}

	public List<InsulinOrderEntity> getInsulinOrder(int patient_id, int type) {
		Connection conn = null;
		List<InsulinOrderEntity> list = null;
		try {
			conn = DBConnection.getDBConnection();
			list = insulinOrderDAO.listByPatientId(conn, patient_id, type);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return list;
	}
}
